<template>
  <div class="forgot-container">
    <div class="forgot-background">
      <div class="forgot-diagonal"></div>
    </div>
    <div class="forgot-form-container">
      <div class="forgot-header">
        <h1>找回密码</h1>
        <h2>Reset Your Password</h2>
      </div>

      <el-form
        class="forgot-form"
        :model="forgotForm"
        :rules="forgotRules"
        ref="forgotFormRef"
      >
        <el-form-item prop="username">
          <el-input
            v-model="forgotForm.username"
            placeholder="请输入用户名"
            prefix-icon="User"
            clearable
          ></el-input>
        </el-form-item>

        <el-form-item prop="email">
          <el-input
            v-model="forgotForm.email"
            type="email"
            placeholder="请输入注册邮箱"
            prefix-icon="Message"
            clearable
          ></el-input>
        </el-form-item>

        <el-form-item prop="verificationCode">
          <div class="verification-code">
            <el-input
              v-model="forgotForm.verificationCode"
              placeholder="请输入验证码"
              prefix-icon="Key"
              clearable
            ></el-input>
            <el-button
              type="primary"
              plain
              class="send-code-btn"
              @click="sendVerificationCode"
              :disabled="isCountingDown"
            >
              {{ countdown > 0 ? `${countdown}s后重新获取` : "获取验证码" }}
            </el-button>
          </div>
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            class="submit-btn"
            @click="submitForm"
            :loading="loading"
          >
            发送重置链接
          </el-button>
        </el-form-item>
      </el-form>

      <div class="success-message" v-if="sendSuccess">
        <el-icon color="#67C23A" :size="24">
          <CircleCheck />
        </el-icon>
        <span>重置链接已发送至您的邮箱，请查收邮件并按照指引操作</span>
      </div>

      <div class="forgot-footer">
        <router-link to="/login" class="back-home">
          <el-icon>
            <ArrowLeft />
          </el-icon>
          已重置，点击返回登录
        </router-link>
        <div class="copyright">© 2023 MES制造执行系统 版权所有</div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from "vue";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";
import {
  ArrowLeft,
  CircleCheck,
  User,
  Message,
  Key,
} from "@element-plus/icons-vue";
import { postForgetpwdApi } from "@/api/Forgetpwd";

const router = useRouter();

// 表单数据
const forgotForm = ref({
  username: "",
  email: "",
  verificationCode: "",
});

// 验证规则
const forgotRules = {
  username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  email: [
    { required: true, message: "请输入邮箱", trigger: "blur" },
    {
      type: "email",
      message: "请输入正确的邮箱格式",
      trigger: ["blur", "change"],
    },
  ],
  verificationCode: [
    { required: true, message: "请输入验证码", trigger: "blur" },
    {
      pattern: /^\d{6}$/,
      message: "验证码为6位数字",
      trigger: ["blur", "change"],
    },
  ],
};

const loading = ref(false);
const forgotFormRef = ref();
const countdown = ref(0);
const isCountingDown = ref(false);
const sendSuccess = ref(false);

// 发送验证码
const sendVerificationCode = () => {
  if (!forgotForm.value.username || !forgotForm.value.email) {
    ElMessage.warning("请先填写用户名和邮箱");
    return;
  }

  // 模拟发送验证码
  isCountingDown.value = true;
  countdown.value = 60;

  const timer = setInterval(() => {
    countdown.value--;
    if (countdown.value <= 0) {
      clearInterval(timer);
      isCountingDown.value = false;
    }
  }, 1000);

  ElMessage.success("验证码已发送到您的邮箱，请注意查收");
};

// 提交表单
const submitForm = () => {
  forgotFormRef.value.validate(async (valid: boolean) => {
    if (valid) {
      try {
        loading.value = true;
        const res: any = await postForgetpwdApi({
          email: forgotForm.value.email,
        });
        ElMessage.success("密码重置链接已发送至您的邮箱，请注意查收");
      } catch (err: any) {
        console.log(err);
        ElMessage.error("请检查邮箱是否正确");
      } finally {
        loading.value = false;
      }
    }
  });
};
</script>

<style scoped>
.forgot-container {
  display: flex;
  height: 100vh;
  width: 100vw;
  overflow: hidden;
}

.forgot-background {
  flex: 1;
  background: url("https://example.com/mes-background.jpg") no-repeat center
    center;
  background-size: cover;
  position: relative;
}

.forgot-diagonal {
  position: absolute;
  top: 0;
  right: 0;
  width: 200%;
  height: 200%;
  background: linear-gradient(
    to bottom left,
    transparent 0%,
    transparent 50%,
    rgba(27, 154, 238, 0.8) 50%,
    rgba(27, 154, 238, 0.8) 100%
  );
  transform: translateX(50%) translateY(-50%) rotate(45deg);
  transform-origin: center center;
  z-index: 1;
}

.forgot-form-container {
  width: 550px;
  background: white;
  padding: 60px 40px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  box-shadow: -5px 0 15px rgba(0, 0, 0, 0.1);
  z-index: 2;
}

.forgot-header {
  text-align: center;
  margin-bottom: 30px;
}

.forgot-header h1 {
  font-size: 24px;
  color: #1b9aee;
  margin-bottom: 10px;
}

.forgot-header h2 {
  font-size: 16px;
  color: #666;
  font-weight: normal;
}

.forgot-form {
  width: 100%;
}

/* 输入框样式 */
.forgot-form :deep(.el-input__wrapper) {
  height: 48px;
  border-radius: 6px;
  border: 1px solid #e0e0e0;
  transition: all 0.3s;
}

.forgot-form :deep(.el-input__wrapper.is-focus) {
  border-color: #1b9aee;
  box-shadow: 0 0 0 2px rgba(23, 92, 180, 0.2);
}

.forgot-form :deep(.el-input__inner) {
  font-size: 14px;
  color: #333;
}

.forgot-form :deep(.el-input__prefix) {
  color: #1b9aee;
  margin-left: 10px;
}

/* 验证码输入区域 */
.verification-code {
  display: flex;
  gap: 10px;
}

.send-code-btn {
  width: 140px;
  flex-shrink: 0;
  margin-top: 10px;
  border-color: #1b9aee;
  color: #1b9aee;
}

.send-code-btn:hover {
  background-color: rgba(23, 92, 180, 0.1);
}

.send-code-btn:disabled {
  color: #999;
  border-color: #e0e0e0;
  background-color: #f5f5f5;
}

/* 按钮样式 */
.submit-btn {
  width: 100%;
  height: 48px;
  font-size: 16px;
  background-color: #1b9aee;
  border-color: #1b9aee;
  border-radius: 6px;
  transition: all 0.3s;
}

.submit-btn:hover {
  background-color: #1a6fd8;
  border-color: #1a6fd8;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(23, 92, 180, 0.2);
}

.submit-btn:active {
  transform: translateY(0);
}

/* 成功消息 */
.success-message {
  margin-top: 20px;
  padding: 15px;
  background-color: #f6ffed;
  border: 1px solid #b7eb8f;
  border-radius: 4px;
  display: flex;
  align-items: center;
  color: #52c41a;
}

.success-message .el-icon {
  margin-right: 10px;
}

/* 页脚 */
.forgot-footer {
  margin-top: 30px;
  text-align: center;
}

.back-home {
  display: inline-flex;
  align-items: center;
  color: #1b9aee;
  text-decoration: none;
  margin-bottom: 15px;
}

.back-home .el-icon {
  margin-right: 5px;
}

.back-home:hover {
  text-decoration: underline;
}

.copyright {
  color: #999;
  font-size: 12px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .forgot-container {
    flex-direction: column;
  }

  .forgot-background {
    display: none;
  }

  .forgot-form-container {
    width: 100%;
    padding: 40px 20px;
  }

  .verification-code {
    flex-direction: column;
  }

  .send-code-btn {
    width: 100%;
  }
}
</style>
